import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../util/toast_util.dart';
import '../../../../res/colors.dart';
import '../../../../widget/ripple_widget.dart';
import '../res/style.dart';
import '../util/keyboard_util.dart';

class DataPickerTool extends StatefulWidget {
  String title;
  RxString selectTitle;
  String placeholder;
  GestureTapCallback? clearTap;
  bool isRequired;
  List dataArr;
  Function(Map)? onChange;
  Function()? errorTap;

  DataPickerTool(
      {Key? key,
      required this.title,
      required this.selectTitle,
      required this.dataArr,
      this.placeholder = '',
      this.isRequired = false,
      this.onChange,
      this.errorTap,
      this.clearTap})
      : super(key: key);

  @override
  State<DataPickerTool> createState() => _DataPickerToolState();
}

class _DataPickerToolState extends State<DataPickerTool> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      child: GestureDetector(
        onTap: () {
          KeyboardUtils.hideKeyboard(context);
          if (widget.dataArr.isEmpty) {
            if (widget.errorTap != null) {
              widget.errorTap!();
            }

            ToastUtils.show("暂无数据");
            return;
          }
          didClickSelectedGender(context);
        },
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            SizedBox(
              width: 90.w,
              child: Text(
                widget.title,
                style: Styles.style_9F9EA6_16,
              ),
            ),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Obx(() {
                    return SizedBox(
                      width: double.infinity,
                      child: Text(
                        widget.selectTitle.value,
                        style: Styles.style_red_16,
                        textAlign: TextAlign.left,
                        // overflow: TextOverflow.ellipsis,
                      ),
                    );
                  }),
                ],
              ),
            ),
            widget.placeholder.isEmpty ||
                    widget.placeholder == widget.selectTitle.value
                ? const Icon(
                    Icons.chevron_right,
                    color: ColorStyle.color_home,
                  )
                : Ripple(
                    onTap: widget.clearTap,
                    child: const Padding(
                      padding: EdgeInsets.all(5),
                      child: Icon(
                        Icons.cancel_rounded,
                        color: ColorStyle.color_home,
                        size: 18,
                      ),
                    )),
          ],
        ),
      ),
    );
  }

  void didClickSelectedGender(BuildContext context) {
    int selectedValue = 0;

    showCupertinoModalPopup(
        context: context,
        builder: (BuildContext context) {
          return Container(
            color: Colors.white,
            height: 250,
            child: Column(
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                      },
                      child: Padding(
                        padding: const EdgeInsets.only(left: 10, top: 10),
                        child: Text(
                          "取消",
                          style: Styles.style_black_16,
                        ),
                      ),
                    ),
                    GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                        widget.onChange!(widget.dataArr[selectedValue]);
                      },
                      child: Padding(
                        padding: const EdgeInsets.only(right: 10, top: 10),
                        child: Text(
                          "确定",
                          style: Styles.style_black_16,
                        ),
                      ),
                    ),
                  ],
                ),
                DividerStyle.dividerGrayHalf,
                Expanded(
                  child: DefaultTextStyle(
                    style: const TextStyle(
                      color: Colors.black,
                      fontSize: 17,
                    ),
                    child: CupertinoPicker(
                      backgroundColor: Colors.white,
                      itemExtent: 40,
                      onSelectedItemChanged: (value) {
                        selectedValue = value;
                      },
                      children: widget.dataArr.map((data) {
                        return Center(
                            child: Text(
                          data['value'],
                          style: Styles.style_black_16_bold,
                        ));
                      }).toList(),
                    ),
                  ),
                ),
              ],
            ),
          );
        });
  }
}
